package net.neptech.strategy.form;

import cn.hutool.core.util.StrUtil;
import net.neptech.model.vo.FormVO;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author Saya
 * @Date 2025/8/20 15:39
 * @Description TODO
 **/
public class FormStrategyFactory {
    private static final Map<String, FormStrategy> STRATEGY_CONCURRENT_HASH_MAP = new ConcurrentHashMap<>();

    /**
     * 提供获取策略的方法
     */
    public static FormStrategy getStrategy(String key) {
        return STRATEGY_CONCURRENT_HASH_MAP.get(key);
    }

    /**
     * 在Bean属性初始化后执行该方法
     *
     * @param key          表单类型
     * @param formStrategy 表单实现类
     */
    public static void register(String key, FormStrategy formStrategy) {
        STRATEGY_CONCURRENT_HASH_MAP.put(key, formStrategy);
    }

    /**
     * 检查字段格式 ???TODO
     */
    public static void checkValue(List<FormVO> formVOList, Map<String, Object> paramMap) {
        for (FormVO formVO : formVOList) {
            FormStrategy strategy = FormStrategyFactory.getStrategy(formVO.getFormType());
            if (strategy == null) {
                continue;
            }
            Object o = paramMap.get(formVO.getFormId());
            if (o == null || StrUtil.isBlankIfStr(o)) {
                continue;
            }
            strategy.checkValue(o);
        }
    }

    /**
     * 数据长度
     */
    public static int length(String s, String formType) {
        if (StrUtil.isBlank(s)) {
            return 0;
        }
        return getStrategy(formType).length(s);
    }
}
